//
//  CommentCellFrameLayout.m
//  sensen_ios
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "CommentCellFrameLayout.h"
#import "ZKTextImgReplaceHandle.h"
#define kImgMargin 8
@implementation CommentCellFrameLayout

- (void)setModel:(CommentModel *)model {
    
    _model = model;
    
    // 布局
    // 1.头像
    CGFloat iconX = 10;
    CGFloat iconY = 5;
    CGFloat iconW = 30;
    CGFloat iconH = iconW;
    _iconRect = CGRectMake(iconX, iconY, iconW, iconH);
    
    // 2.昵称
    CGFloat nickX = CGRectGetMaxX(_iconRect) + 5;
    CGFloat nickY = CGRectGetMinY(_iconRect);
    CGFloat nickW = [model.nickname getSizeOfString:15].width;
    CGFloat nickH = 18;
    nickW = (nickW < 200) ? nickW : 200;
    if (SCREEN_WIDTH == 320) {
        nickW = 160;
    }
    _nickRect = CGRectMake(nickX, nickY, nickW, nickH);
    _nickRect2 = CGRectMake(nickX, nickY, [@"禁言用户" getSizeOfString:15].width, nickH);
    //楼主
    CGFloat louX = CGRectGetMaxX(_nickRect) + 5;
    CGFloat louY = CGRectGetMinY(_iconRect);
    CGFloat louW = 30;
    CGFloat louH = 15;
    _louImageRect = CGRectMake(louX, louY, louW, louH);
    
    //楼层
    CGFloat nubX = CGRectGetMinX(_nickRect);
    CGFloat nubY = CGRectGetMaxY(_nickRect);
    CGFloat nubW = 40;
    CGFloat nubH = iconH - nickH;
    _nubRect = CGRectMake(nubX, nubY, nubW, nubH);
    
    // 3.评论创建时间
    CGFloat timeX = CGRectGetMaxX(_nubRect) + 5;
    CGFloat timeY = CGRectGetMaxY(_nickRect);
    CGFloat timeW = [model.dateline_show getSizeOfString:12].width;
    CGFloat timeH = iconH - nickH;
    _createTimeRect = CGRectMake(timeX, timeY, timeW, timeH);
    
    // 4.评论图片
    CGFloat commentImgX;
    CGFloat commentImgY = 10;
    CGFloat commentImgW = 20;
    CGFloat commentImgH = commentImgW;
    commentImgX = SCREEN_WIDTH - 10 - commentImgW;
    _commentImgRect = CGRectMake(commentImgX, commentImgY, commentImgW, commentImgH);
    
    //5.删除评论按钮
    CGFloat deleButtonX = SCREEN_WIDTH - 70;
    CGFloat deleButtonY = 10;
    CGFloat deleButtonW = 30;
    CGFloat deleButtonH = 20;
    _deleButtonRect = CGRectMake(deleButtonX, deleButtonY, deleButtonW, deleButtonH);
  // 6.内容
    CGFloat contentX = CGRectGetMinX(_nubRect);
    CGFloat contentY = CGRectGetMaxY(_nubRect) + 5;
    CGFloat contentW = SCREEN_WIDTH - contentX - 10;
//    CGFloat contentH = [_model.content getBoundingRectOfStringSize:CGSizeMake(contentW, 10000) font:13].size.height;
    CGFloat contentH = [ZKTextImgReplaceHandle getImgTextHeightWithText:_model.content regex:nil size:CGSizeMake(contentW, 10000) fontSize:15];
    contentH = (contentH < 20) ? 20 : contentH;
    _contentRect = CGRectMake(contentX, contentY, contentW, contentH);
 
    
    CGFloat imgX = 15;
    CGFloat imgY = CGRectGetMaxY(_contentRect) + 5;
    CGFloat imgW = SCREEN_WIDTH - 30;
    CGFloat imgH = 0;
    NSMutableArray *imgHAry = [NSMutableArray array];
    for (NSString *urlStr in _model.imgAry) {
        CGSize imgSize = [UIImage getImageSizeWithURL:urlStr];
        CGFloat fitH = (imgSize.height / imgSize.width) * SCREEN_WIDTH;
        [imgHAry addObject:@(fitH)]; // 吧图片高度存入数组中
        imgH += (fitH + kImgMargin);
    }
    _imgHeightAry = [imgHAry copy];
    _imgRect = CGRectMake(imgX, imgY, imgW, imgH);
    //回复内部视图
    CGFloat reLab1X = 10;
    CGFloat reLab1Y = 10;
    CGFloat reLab1W = SCREEN_WIDTH - 75;
//    CGFloat reLab1H = [_model.reply1 getBoundingRectOfStringSize:CGSizeMake(contentW, 10000) font:12].size.height;
    CGFloat reLab1H = [ZKTextImgReplaceHandle getImgTextHeightWithText:_model.reply1 regex:nil size:CGSizeMake(reLab1W, 10000) fontSize:13];
    if (reLab1H < 20) {
        reLab1H = 20;
    }
    _replyLabRect1 = CGRectMake(reLab1X, reLab1Y, reLab1W, reLab1H);
    CGFloat reLab2X = 10;
    CGFloat reLab2Y = CGRectGetMaxY(_replyLabRect1) + 5;
    CGFloat reLab2W = SCREEN_WIDTH - 75;
//    CGFloat reLab2H = [_model.reply2 getBoundingRectOfStringSize:CGSizeMake(contentW, 10000) font:12].size.height;
    CGFloat reLab2H = [ZKTextImgReplaceHandle getImgTextHeightWithText:_model.reply2 regex:nil size:CGSizeMake(reLab2W , 10000) fontSize:13];
    if (reLab2H < 20) {
        reLab2H = 20;
    }
    _replyLabRect2 = CGRectMake(reLab2X, reLab2Y, reLab2W, reLab2H);
    
    CGFloat showButtX = 10;
    CGFloat showButtY = CGRectGetMaxY(_replyLabRect2) + 5;
    CGFloat showButtW = SCREEN_WIDTH - 75;
    CGFloat showButtH = 12;
    _showMoreButtonRect = CGRectMake(showButtX, showButtY, showButtW, showButtH);
    
    //回复视图
    CGFloat bgViewX = CGRectGetMinX(_nubRect);
    CGFloat bgViewY = CGRectGetMaxY(_imgRect) + 5;
    CGFloat H;
    if (_model.comment == 0) {
        H = 0;
    }else{
        if ([_model.comment_num intValue] == 1) {
            H = CGRectGetMaxY(_replyLabRect1) + 10;
        }else if ([_model.comment_num intValue] == 2){
            H = CGRectGetMaxY(_replyLabRect2) + 10;
        }else{
            H = CGRectGetMaxY(_showMoreButtonRect) + 10;
        }
    }
    CGFloat bgViewW = SCREEN_WIDTH - 10 - bgViewX;
    CGFloat bgViewH = H;
    _bgViewRect = CGRectMake(bgViewX, bgViewY, bgViewW, bgViewH);
    
    // 7.单元格尺寸
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = SCREEN_WIDTH;
    if (_model.comment == 0) {
        CGFloat h = CGRectGetMaxY(_imgRect) + 15;
        _cellRect = CGRectMake(x, y, w, h);
    }else{
        CGFloat h = CGRectGetMaxY(_bgViewRect) + 15;
        _cellRect = CGRectMake(x, y, w, h);
    }

}

@end
